<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Agency\Config\Timezone;
use App\Models\Agency\Auth\UserConfig;

class CreateConfigTimezonesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $tableName = Timezone::tableName();

        Timezone::schemaConnection()->create($tableName, function (Blueprint $table) {
            $table->id('id');
            $table->string('name', 128)->comment('时区名称');
            $table->string('code', 32)->comment('UTC偏移 (UTC+8:00)')->index();
            $table->string('timezone', 32)->comment('时区代码 (Etc/GMT-8)')->unique();

            $table->unsignedInteger('deleted')->nullable()->index();
            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');
        });

        Timezone::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '配置--时区列表'");

        // 用户配置表添加时区字段
        UserConfig::schemaConnection()->table(UserConfig::tableName(), function (Blueprint $table) {
            $table->string('timezone', 32)->comment('时区代码')->nullable()->index();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Timezone::schemaConnection()->dropIfExists(Timezone::tableName());

        // 用户配置表删除时区字段
        UserConfig::schemaConnection()->table(UserConfig::tableName(), function (Blueprint $table) {
            $table->dropColumn('timezone');
        });
    }
}
